package com.example.springboot.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.springboot.service.ITransportService;
import com.example.springboot.entity.Transport;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小申
 * @since 2024-03-12
 */
@RestController
@RequestMapping("//transport")
        public class TransportController {
    
    @Resource
    private ITransportService transportService;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Transport transport){
        return Result.success(transportService.saveOrUpdate(transport));
    }

    //删除单条数据
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        return Result.success(transportService.removeById(id));
    }

    //批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        return Result.success(transportService.removeByIds(ids));
    }

    //获取全部数据
    @GetMapping
    public Result findAll(){
        return Result.success(transportService.list());
    }

    //获取单条数据
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id){
        return Result.success(transportService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize){
        QueryWrapper<Transport> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(transportService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }
    }

